package com.yq.controller;

import com.google.gson.Gson;
import com.sun.org.apache.regexp.internal.REUtil;
import com.yq.model.Result;
import com.yq.model.bo.*;
import com.yq.model.vo.AdminGoodsTypeVO;
import com.yq.model.vo.GetGoodsByIdVO;
import com.yq.model.vo.GoodsVO;
import com.yq.model.vo.NoReplyMsgVO;
import com.yq.model.vo.entity.GoodsEntity;
import com.yq.model.vo.entity.SpecsEntity;
import com.yq.service.AdminGoodsService;
import com.yq.service.impl.AdminGoodsServiceImpl;
import com.yq.utils.FileUploadUtils;
import com.yq.utils.HttpUtils;
import jdk.nashorn.internal.runtime.ECMAException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/api/admin/goods/*")
public class AdminGoodsServlet extends HttpServlet {
    AdminGoodsService adminGoodsService = new AdminGoodsServiceImpl();
    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/goods/", "");
        if ("addGoods".equals(op)) {
            addGoods(request, response);
        } else if ("imgUpload".equals(op)) {
            imgUpload(request, response);
        } else if ("addType".equals(op)) {
            addType(request, response);
        } else if ("updateGoods".equals(op)) {
            updateGoods(request, response);
        }else if("deleteSpec".equals(op)){
            deleteSpec(request,response);
        }else if ("addSpec".equals(op)){
            addSpec(request,response);
        }else if ("reply".equals(op)){
            reply(request,response);
        }

    }

    /**
     * 消息的回复
     * @param request
     * @param response
     */
    private void reply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestInfo = HttpUtils.getRequestInfo(request);
        ReplyBO replyBO = gson.fromJson(requestInfo, ReplyBO.class);
        int code = adminGoodsService.replay(replyBO);
        Result result = null;
        if (code == 200) {
            result = Result.ok();
        } else if (code == 404) {
            result = Result.error("回复失败！！！");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 添加商品的规格 /api/admin/goods/addSpec  不做处理
     * @param request
     * @param response
     */
    private void addSpec(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestInfo = HttpUtils.getRequestInfo(request);
        AddSpecBO addSpecBO =null;
        try {
             addSpecBO = gson.fromJson(requestInfo, AddSpecBO.class);
        }catch (Exception e){
            response.getWriter().println(gson.toJson(Result.error("参数不合法")));
            return;
        }
        response.getWriter().println(gson.toJson(Result.ok(addSpecBO)));
    }
    /**
     * 修改商品的时候删除规格的操作/api/admin/goods/deleteSpec  不做处理
     * @param request
     * @param response
     */
    private void deleteSpec(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 商品信息的修改  先删除所有的spec中的数据，添加进去
     * @param request
     * @param response
     */
    private void updateGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestInfo = HttpUtils.getRequestInfo(request);
        UpdateGoodsBO updateGoodsBO = null;
        try {
            updateGoodsBO = gson.fromJson(requestInfo, UpdateGoodsBO.class);

        } catch (Exception e) {
            response.getWriter().println(gson.toJson(Result.error("参数不合法")));
            return;
        }
        //获取到信息 修改
        int code = adminGoodsService.updateGoods(updateGoodsBO);
        Result result = null;
        if (code == 200) {
            result = Result.ok();
        } else if (code == 404) {
            result = Result.error("修改失败！！！");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 添加商品的分类 /api/admin/goods/addType
     *
     * @param request
     * @param response
     */
    private void addType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestInfo = HttpUtils.getRequestInfo(request);
        //TODO {"name":"111"}
        AddTypeBO addTypeBO = null;
        try {
            addTypeBO = gson.fromJson(requestInfo, AddTypeBO.class);
        } catch (Exception e) {
            response.getWriter().println(gson.toJson(Result.error("参数并不合法")));
            return;
        }
        int code = adminGoodsService.addGoodsType(addTypeBO);
        Result result = null;
        if (code == 200) {
            result = Result.ok();

        } else if (code == 404) {
            result = Result.error("该类目已经存在！！！");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 商品的添加之前，先上传图片
     *
     * @param request
     * @param response
     */
    private void imgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //文件的上传
        Map<String, String> map = FileUploadUtils.parseRequest(request);
        String file = map.get("file");
        response.getWriter().println(gson.toJson(Result.ok(file)));
    }


    /**
     * 添加商品
     *
     * @param request
     * @param response
     */
    private void addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 1 获取商品的数据
        String requestInfo = HttpUtils.getRequestInfo(request);
        AdminAddGoodsBO adminAddGoodsBO = null;
        try {
            adminAddGoodsBO = gson.fromJson(requestInfo, AdminAddGoodsBO.class);
        } catch (Exception e) {
            response.getWriter().println(gson.toJson(Result.error("参数不合法")));
            return;
        }
        int code = adminGoodsService.addGoods(adminAddGoodsBO);
        Result result = null;
        if (code == 200) {
            result = Result.ok();
        } else if (code == 404) {
            result = Result.error("失败！");
        }
        response.getWriter().println(gson.toJson(result));
    }


    /**
     * 获取商品的种类
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // {"id":1,"name":"女装"}
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/goods/", "");
        if ("getType".equals(op)) {
            getType(request, response);
        } else if ("getGoodsByType".equals(op)) {
            getGoodsByType(request, response);
        } else if ("deleteGoods".equals(op)) {
            deleteGoods(request, response);
        } else if ("getGoodsInfo".equals(op)) {
            getGoodsInfo(request, response);
        } else if ("deleteType".equals(op)) {
            deleteType(request, response);
        }else if ("noReplyMsg".equals(op)){
            noReplyMsg(request,response);
        }else if ("repliedMsg".equals(op)){
            repliedMsg(request,response);
        }
    }

    /**
     * 消息已回复
     * @param request
     * @param response
     */
    private void repliedMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int state=0;
        List<NoReplyMsgVO> list = adminGoodsService.repliedMsg(state);
        response.getWriter().println(gson.toJson(Result.ok(list)));

    }

    /**
     * 没有回复的留言信息
     * @param request
     * @param response
     */
    private void noReplyMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int state=1;
        List<NoReplyMsgVO> list = adminGoodsService.getNoReplyMsg(state);
        response.getWriter().println(gson.toJson(Result.ok(list)));
    }

    /**
     * 删除商品的分类/api/admin/goods/deleteType?typeId=4
     *
     * @param request
     * @param response
     */
    private void deleteType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");

        int tId = 0;
        try {
            tId = Integer.parseInt(typeId);
        } catch (Exception e) {
            response.getWriter().println(gson.toJson(Result.error("参数不合法")));
            return;
        }

        int code = adminGoodsService.deleteGoodsTypeById(tId);
        Result result = null;
        if (code == 200) {
            result = Result.ok();

        } else if (code == 404) {
            result = Result.error("删除失败！！");
        }
        response.getWriter().println(gson.toJson(result));

    }

    /**
     * 点击编辑显示商品的详细信息/api/admin/goods/getGoodsInfo?id=4
     *
     * @param request
     * @param response
     */
    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int goodsId = Integer.parseInt(id);
        List<GetGoodsByIdVO> getGoodsByIdVO = adminGoodsService.getGoodsInfoById(goodsId);
        GoodsEntity goods = null;
        List<SpecsEntity> specs = new ArrayList<>();
        for (GetGoodsByIdVO goodsByIdVO : getGoodsByIdVO) {
            goods = goodsByIdVO.getGoods();
            specs.addAll(goodsByIdVO.getSpecs());
        }
        GetGoodsByIdVO resultGetGoodsByIdVO = new GetGoodsByIdVO();
        resultGetGoodsByIdVO.setGoods(goods);
        resultGetGoodsByIdVO.setSpecs(specs);
        response.getWriter().println(gson.toJson(Result.ok(resultGetGoodsByIdVO)));
    }

    /**
     * 删除商品/api/admin/goods/deleteGoods?id=1
     *
     * @param request
     * @param response
     */
    private void deleteGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Integer goodsId = Integer.parseInt(id);
        adminGoodsService.deleteGoodsById(goodsId);
        response.getWriter().println(gson.toJson(Result.ok()));

    }

    /**
     * 商品的分类显示///api/admin/goods/getGoodsByType?typeId=1
     *
     * @param request
     * @param response
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        int typeIds = Integer.parseInt(typeId);
        List<GoodsVO> list = adminGoodsService.searchGoodsByTypeIdInfo(typeIds);
        response.getWriter().println(gson.toJson(Result.ok(list)));
    }

    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<AdminGoodsTypeVO> list = adminGoodsService.getGoodsType();
        response.getWriter().println(gson.toJson(Result.ok(list)));
    }
}
